<?php
    require_once ('scripts.php');
    require_once ('model/dao/vo/UserVO.php');
    require_once ('model/dao/vo/PartnerVO.php');
  
    $action = $_REQUEST['action'];

    switch ($action) {
        case 'getSellerVehicle':
 
            $vehicle_key = $_REQUEST['key'];
            $result = VehicleDAOImpl::getInstance()->getBidVehicleDetails($vehicle_key);
            $data = encodeJsonString($result);
            echo  $data ;
            break;
       
        case 'getPartnerVehicle':
$vehicle_key = $_REQUEST['key'];
            $result = VehicleDAOImpl::getInstance()->getShareDetails($vehicle_key);
            $data = encodeJsonString($result);
            echo  $data ;

            break;
		 case 'getUnknowndata':
$vehicle_key = $_REQUEST['key'];
            $result = VehicleDAOImpl::getInstance()->getUnknownShareDetails($vehicle_key);
            $data = encodeJsonString($result);
            echo  $data ;
           break;  
        case 'addNewPartner':
            $partnerVO = new PartnerVO();
            $partnerVO->partner_id = 0;
            $partnerVO->username = $_POST['username'];
            $partnerVO->password = $_POST['password'];
            $partnerVO->f_name = $_POST['f_name'];
            $partnerVO->l_name = $_POST['l_name'];
            $partnerVO->company = $_POST['company'];
            $partnerVO->zip_code = $_POST['zip'];
            $partnerVO->phone_no = $_POST['phone'];
            $partnerVO->email = $_POST['email'];
            $partnerVO->sec_email = $_POST['s_email'];
            $partnerVO->address = $_POST['address'];
            $partnerVO->country = $_POST['country'];
            $partnerVO->city = $_POST['city'];
            $partnerVO->latitude = $_POST['latitude'];
            $partnerVO->longitude = $_POST['longitude'];
            $partnerVO->user_type = $_POST['user_type'];
            $result = VehicleDAOImpl::getInstance()->createNewPartner($partnerVO);
            return $result;
            break;
       
        case 'checkLogin':
            $userVO = new UserVO();
            $userVO->username = $_POST['loginUsername'];
            $userVO->password = $_POST['loginPassword'];
            $result = AdminDAOImpl::getInstance()->loginUser($userVO);
            return $result;
            break;
		
		case 'addNewComment':

                      $messageVO->messageVO = new MessageVO ();
		
			$messageVO->message = $_REQUEST['mess'];

			$messageVO->messageOwener = $_REQUEST['messageOwener'];
			$messageVO->messageRecipient = $_REQUEST['messageRecipient'];
			$messageVO->vehicleId = $_REQUEST['vehicleId'];
			$messageVO->userType = $_REQUEST['userType'];
			$messageVO->messageTo = $_REQUEST['messageTo'];
            $messageVO->offer = 'null';
			
			$colName = 'update_date';
			$reply = $_REQUEST['reply'];
			$doMail = $_REQUEST['doMail'];
			$update='';
			
            $result = VehicleDAOImpl::getInstance()->updateMessageVehicleID($messageVO, $colName, $reply, $doMail, $update);
            if($result)
			{
				$message = VehicleDAOImpl::getInstance()->getAllMessageVehicleID($_REQUEST['vehicleId']) ;
				$data = encodeJsonMessageString($message);
				echo $data;
			}
            break;
case 'getImages':
       $image = VehicleDAOImpl::getInstance()->getAllImageByVehicleID($_REQUEST['vehicleId']) ;
				$data = encodeJsonImageString($image);
				echo $data;
break;
        default:
           
            break;
    }
   function encodeJsonImageString($image)
    {
    	$ar_image = $image->source;
		$images = array();

		for($i=0; $i<count($ar_image); $i++)
		{    
				
			
			$images[] = array(
			"imageId"=>$ar_image[$i]->imageId,
			"imageUrl"=>$ar_image[$i]->imageUrl,
			"thumpUrl"=>$ar_image[$i]->thumpUrl);
		}
		
		$imagedata = array("image"=>$images);

        return json_encode($imagedata);
	}


	function encodeJsonMessageString($message)
    {
    	$ar_message = $message->source;
		$messages = array();

		for($i=0; $i<count($ar_message); $i++)
		{    
				
			$messages[] = array(
			"vehicleId" => $ar_message[$i]->vehicleId,
			"message" => $ar_message[$i]->message,
			"messageOwener" => $ar_message[$i]->messageOwener,
			"messageRecipient" => $ar_message[$i]->messageRecipient,
			"offer" => $ar_message[$i]->offer,
			"isRead" => $ar_message[$i]->isRead,
			"dateTime" => $ar_message[$i]->dateTime);
		}
		
		$messagedata = array("message"=>$messages);

        return json_encode($messagedata);
	}

    function encodeJsonString($vehicle)
    {
        
		$ar_images = $vehicle->image_collection->source;
		$images = array();
		for($i=0; $i<count($ar_images); $i++)
		{
			$images[] = array(
			"imageId"=>$ar_images[$i]->imageId,
			"imageUrl"=>$ar_images[$i]->imageUrl,
			"thumpUrl"=>$ar_images[$i]->thumpUrl);
		}
		
		$ar_message = $vehicle->messages->source;
		$messages = array();
		for($i=0; $i<count($ar_message); $i++)
		{
				
			if($ar_message[$i]!='')
 {
  $messages[] = array(
   "vehicleId" => $ar_message[$i]->vehicleId,
   "message" => $ar_message[$i]->message,
   "messageOwener" => $ar_message[$i]->messageOwener,
   "messageRecipient" => $ar_message[$i]->messageRecipient,
   "offer" => $ar_message[$i]->offer,
   "isRead" => $ar_message[$i]->isRead,
   "dateTime" => $ar_message[$i]->dateTime);
 }
		}

$partnerEmail = '';
$partnerID = '';
$partnerFName = '';
if($vehicle->partner_details != '')
{
 if($vehicle->partner_details->email != null || $vehicle->partner_details->email != '')
{
 $partnerEmail = $vehicle->partner_details->email;
 $partnerID = $vehicle->partner_details->partner_id;
 $partnerFName = $vehicle->partner_details->f_name;
}
}		
		$vehicles[] = array(
		
		"vehicle_id" => $vehicle->vehicle_id,
        "year"=> $vehicle->year,
        "make"=> $vehicle->make,
        "model"=> $vehicle->model,
        "vin"=> $vehicle->vin,
        "mileage"=> $vehicle->mileage,
        "engine"=> $vehicle->engine,
        "transmission"=> $vehicle->transmission,
        "drivetrain_type"=> $vehicle->drivetrain_type,
        "title_note"=> $vehicle->title_note,
        "wrong"=> $vehicle->wrong,
        "great"=> $vehicle->great,
        "i_want_to"=> $vehicle->i_want_to,
        "bid_price"=> $vehicle->bid_price,
        "asking_price"=> $vehicle->asking_price,
        "accepted_price"=> $vehicle->accepted_price,
        "removal_date"=> $vehicle->removal_date,
        "first_name"=> $vehicle->first_name,
        "email"=> $vehicle->email,
        "phone"=> $vehicle->phone,
        "cell_phone"=> $vehicle->cell_phone,
        "contact_me"=> $vehicle->contact_me,
        "located_at"=> $vehicle->located_at,
        "paper_work"=> $vehicle->paper_work,
        "street_address"=> $vehicle->street_address,
        "city"=> $vehicle->city,
        "state"=> $vehicle->state,
        "zip"=> $vehicle->zip,
        "v_street_address"=> $vehicle->v_street_address,
        "v_city"=> $vehicle->v_city,
        "v_state"=> $vehicle->v_state,
        "v_zip"=> $vehicle->v_zip,
        "contact_name"=> $vehicle->contact_name,
        "contact_phone"=> $vehicle->contact_phone,
        "delivery"=> $vehicle->delivery,
        "delivery_note"=> $vehicle->delivery_note,
        "delivery_option"=> $vehicle->delivery_option,
   
        "private_note"=> $vehicle->private_note,
        "contact_note"=> $vehicle->contact_note,
   
        "status"=> $vehicle->status,
        "current_action"=> $vehicle->current_action,
        "update_date"=> $vehicle->update_date,
        "date"=> $vehicle->date,
       
       
        "title_name"=> $vehicle->title_name,
        "ad_source"=> $vehicle->ad_source,
        "color"=> $vehicle->color,
"partner_email"=>$partnerEmail,
"partner_name" =>$partnerFName,
"partner_id"=>$partnerID,
        "bussiness_name"=> $vehicle->bussiness_name,
	  "messages"=> $messages, 
	  "images"=> $images 
        );

	   $vehicledata = array("vehicle"=>$vehicles);

       return json_encode($vehicledata);
    }
?>